﻿Public Class SettingWindow

    Private Sub SettingWindow_Loaded(sender As Object, e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        Me.CommandBindings.Add(New CommandBinding(Commands.DoOAuth, New ExecutedRoutedEventHandler(AddressOf DoOAuth_Executed)))

    End Sub

    Private Sub DoOAuth_Executed(sender As Object, e As System.Windows.RoutedEventArgs)
        Dim client = New TwitterOAuth(ComsumerKey, ComsumerSecret)
        Dim token As String = String.Empty
        Dim tokenSecret As String = String.Empty
        Try
            If client.GetRequestToken(token, tokenSecret) Then
                client.AddToken(token, tokenSecret)
                If Not client.VerifyCredentials() Then
                    client.AddToken(token, String.Empty)
                    MessageBox.Show("即将打开浏览器并导航至Twitter，请确认您能正确连接", "确认")
                    Process.Start(client.RedirectToAuthorize(token))
                    Dim PW As New PinInputWindow
                    If PW.ShowDialog = True Then
                        Dim verifier = PW.PIN
                        If Not client.GetAccessToken(token, tokenSecret, verifier) Then
                            Return
                        End If
                    Else
                        Return
                    End If
                End If
            End If
        Catch ex As Exception
            MessageBox.Show("连接错误，请检查设置！", "出错了！")
        End Try

        My.Settings.Token = token
        My.Settings.TokenSecret = tokenSecret
    End Sub

End Class
